package test.socket;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.junit.Test;

import hyl.base.net.iot.ABsrv;
import hyl.base.net.iot.Conf;
import hyl.base.net.iot.Csrv;

import hyl.base.net.iot.Netpg3;
import hyl.core.MyCharset;
import hyl.core.MyDate;
import hyl.core.MyFun;
import hyl.core.net.NioClient;

public class testiot {
	InetSocketAddress addrc=new InetSocketAddress("127.0.0.1",9090);
	InetSocketAddress addrb=new InetSocketAddress("127.0.0.1",9080);
	InetSocketAddress addra=new InetSocketAddress("127.0.0.1",9070);

	ABsrv asrv;
	public void startA(InetSocketAddress address) {		
		//启动c服务器
		asrv = new ABsrv ("1001",120000,address,addra);

		asrv.h上行后置器=(reader, bte)->{
			Netpg3 npNetpg3 = new Netpg3();
			byte[] sy = npNetpg3.deserial(bte);
			System.out.println("测试A:收到B端的反馈 "+ npNetpg3);
			return false;
		};
		asrv.start();			
		//启动d终端 连接c
	}
	
	
	ABsrv bsrv;
	public void startB(InetSocketAddress address) {		
		//启动c服务器
		bsrv = new ABsrv ("2001",120000,address,addrb);
		bsrv.h上行后置器=(reader, bte)->{
			Netpg3 npNetpg3 = new Netpg3();
			byte[] sy = npNetpg3.deserial(bte);
			System.out.println("测试B:收到C端的反馈 "+ npNetpg3);
			return false;
		};
		bsrv.start();			
		//启动d终端 连接c
	}
	
	Csrv csrv;
	public void startC(InetSocketAddress address) {		
		//启动c服务器
		csrv = new Csrv ("3001",120000,address,addrc);
		csrv.h上行后置器=(reader, bte)->{
			System.out.println("测试C:收到D端的反馈 "+new String(bte,Conf.D编码));
			return false;
		};
		csrv.start();		

	}
	
	NioClient cliD ;
	public void startD() {
		cliD = new NioClient(addrc);		
		cliD.setHandler((read, data) -> {			
			String cmd=new String(data,Conf.D编码);
			System.out.println("测试D:收到C端发送的信息: "+cmd);
			if (cmd.startsWith("did")) {
				return false;
			}
			d的应答(cmd.replace(';', '?')+"ok;");
			return false;
		});
		cliD.start();
		byte[] dname = MyFun.str2Bytes("4001", Conf.D编码);
		try {
			cliD.send(dname);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	public void a的发送(int i) {
		Netpg3 pg3=new Netpg3();
		pg3.ii路径=new int[] {4001,3001,2001,1001};
		pg3.b指令1=Netpg3.CMD_文本;
		pg3.s请求="open="+i+";";
		asrv.sendToBC(pg3);
	}
	public void b的发送(int i) {
		Netpg3 pg3=new Netpg3();
		pg3.ii路径=new int[] {4001,3001,2001};
		pg3.b指令1=Netpg3.CMD_文本;
		pg3.s请求="open="+i+";";
		bsrv.sendToBC(pg3);
	}
	public void c的发送(int i) {
		Netpg3 pg3=new Netpg3();
		pg3.ii路径=new int[] {4001,3001};
		pg3.b指令1=Netpg3.CMD_文本;
		pg3.s请求="open="+i+";";
		csrv.sendToD(pg3);
	}
	public void d的应答(String cmd) {	
		byte[] fk= MyFun.str2Bytes(cmd, Conf.D编码);
		try {
			cliD.send(fk);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		testiot p3 =new testiot();
		//p3.ttdc();
		//p3.ttdcb() ;
		p3.ttdcba();
	}
	
	public void ttdc() {
		//启动d终端 连接 c		
		startC(null);
		startD() ;
		//通过b发送消息给 d
		//通过d发送消息给b
		MyDate.sleep(3000);
		//通过c发送消息给 d	
		for(int i=0;i<5;i++) {
			c的发送(i) ;
		}
	}
	public void ttdcb() {
		//启动b服务器
		//启动c服务器 连接b
		//启动d终端 连接 c
		//通过b发送消息给 d
		//通过d发送消息给b
			
		startB(null);
		startC(addrb);
		startD() ;
		MyDate.sleep(2000);
	//	b的发送(0) ;
//		//通过c发送消息给 d	
		for(int i=0;i<5;i++) {
			b的发送(i) ;
		}
	}

	
	
	public void ttdcba() {
		//启动a服务器
		//启动b服务器 连接a
		//启动c服务器 连接b
		//启动d终端 连接 c
		//通过a发送消息给 d
		//通过d发送消息给a
	
		startA(null);
		startB(addra);
		startC(addrb);
		startD() ;
		MyDate.sleep(3000);
		//通过c发送消息给 d	
		for(int i=0;i<5;i++) {
			a的发送(i) ;
		}
	}
	//@Test 
	public void test() {
	
		Netpg3 p3=new Netpg3();
		int pp1[]=new int[] {2,4,5};
		p3.set(pp1,Netpg3.CMD_文本,"isline;", MyCharset._UTF8, MyFun.str2Bytes("no;", MyCharset._UTF8));
	
		Netpg3 p4=new Netpg3();
		byte[] sy=p4.deserial(p3.serial());
		for(int i: p4.ii路径) {
			MyFun.print(i) ;
		}
		MyFun.print(p4.ii路径) ;
		MyFun.print(p4.c结果编码) ;
		MyFun.print(p4.b指令1) ;
		MyFun.print(p4.getS反馈()) ;
		System.out.println(sy.length);
	}
	//@Test 
	public void test2() {
		Netpg3 p2=new Netpg3();
		int pp[]=new int[] {22,34,54};
		int isline=100001;
		p2.set(pp,Netpg3.CMD_文本,"isline;", MyCharset._UTF8, MyFun.str2Bytes("no;", MyCharset._UTF8));
		Netpg3 p3=new Netpg3();
		int pp1[]=new int[] {2,4,5};
		int open=100001;
		p3.set(pp1,Netpg3.CMD_文本,"open=1;", MyCharset._UTF8, MyFun.str2Bytes("ok;", MyCharset._UTF8));
		byte[] bb=MyFun.concat(p2.serial(),p3.serial());
		Netpg3 p4=new Netpg3();
		byte[] sy=p4.deserial(bb);
		for(int i: p4.ii路径) {
			MyFun.print(i) ;
		}
		MyFun.print(p4.c结果编码) ;

		MyFun.print(p4.getS反馈()) ;
		System.out.println("----");
		p4.deserial(sy);
		for(int i: p4.ii路径) {
			MyFun.print(i) ;
		}
		MyFun.print(p4.c结果编码) ;
		MyFun.print(p4.b指令1) ;
		MyFun.print(p4.getS反馈()) ;
	}
}
